package cn.itsource.pethome.system.dictionary.contrllor;

import cn.itsource.pethome.system.dictionary.domain.Systemdictionarydetail;
import cn.itsource.pethome.system.dictionary.query.SystemdictionarydetailQuery;
import cn.itsource.pethome.system.dictionary.service.ISystemdictionarydetailService;
import cn.itsource.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cnm")
@Api(value = "数据字典明细的API",description="数据字典明细的CRUD功能")
public class SystemdictionarydetailContrllor {
    @Autowired
    private ISystemdictionarydetailService systemdictionarydetailService;

    @GetMapping("/list")
    @ApiOperation(value = "查询数据字典明细所有数据" )
    public List<Systemdictionarydetail> findAll(){
        return systemdictionarydetailService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据Id查询数据字典明细所有数据" )
    public Systemdictionarydetail findById(@PathVariable  Long id){
        return systemdictionarydetailService.findnoe(id);
    }

    @PostMapping("pagelist")
    @ApiOperation(value = "分页查询数据字典明细所有数据" )
    public AjaxResult pageData(@RequestBody SystemdictionarydetailQuery query){
        System.out.println(query);
        try {
            List<Systemdictionarydetail> employees = systemdictionarydetailService.queryData(query);//获取到所有数据
            Long count = systemdictionarydetailService.queryCount(query);//获取到所有数据
            return AjaxResult.success().setSuccess(true).setData(employees).setTotal(count);//将获取的数据和总条数返回给前端
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success().setSuccess(false).setMsg("操作失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除数据字典明细数据")
    public AjaxResult delete(@PathVariable Long id){
        try {
            systemdictionarydetailService.delete(id);
            return  AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success().setSuccess(false).setMsg("操作失败");
    }

    @PutMapping
    @ApiOperation(value = "根据id修改数据字典明细数据，没有id就添加数据")
    public AjaxResult save(@RequestBody Systemdictionarydetail employee){
        System.out.println(employee);
        try {
            if (employee.getId() != null) {
                systemdictionarydetailService.update(employee);
            }else {
                systemdictionarydetailService.add(employee);
            }
            return AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success().setSuccess(false).setMsg("操作失败");
    }

    @PatchMapping("/del")
    @ApiOperation(value = "批量删除数据字典明细数据")
    public AjaxResult bacthDelete(@RequestBody List<Long> ids){
        try {
            systemdictionarydetailService.batchDel(ids);
            return AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success().setSuccess(false).setMsg("操作失败");
    }
}
